package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * @author konki
 * @time   2018年6月19日 下午7:03:47
 * @email  konki@qq.com
 * @description 
 */
@Controller
@RequestMapping("role")
public class RoleController {
	@Autowired
    private IRoleService roleService;

	@RequestMapping("")
	@PermissionName("角色列表")
	@RequiresPermissions("role:index")
	public String index(){
		return "role/list";
	}

    @RequestMapping("list")
	@ResponseBody
	@PermissionName("角色列表")
	@RequiresPermissions("role:list")
	public PageResult list(QueryObject qo){

        return roleService.query(qo);
	}

    @RequestMapping("queryRoles")
    @ResponseBody
    public List<Role> queryRoles(){
        return roleService.selectAll();
    }
    @RequestMapping("getRoleIdByEmpId")
    @ResponseBody
    public List<Long> queryRoles(Long empId){
        return roleService.getRoleIdByEmpId(empId);
    }

	//保存
    @RequestMapping("save")
    @ResponseBody
    @PermissionName("角色保存")
    @RequiresPermissions("role:save")
    public JsonResult save(Role entity){
        JsonResult result = new JsonResult();
        try{
            roleService.insert(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.markMsg("保存失败");
        }
        return  result;
    }
	//保存
    @RequestMapping("update")
    @ResponseBody
    @PermissionName("角色编辑")
    @RequiresPermissions("role:update")
    public JsonResult update(Role entity){
        JsonResult result = new JsonResult();
        try{
            roleService.updateByPrimaryKey(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.markMsg("更新失败");
        }
        return  result;
    }
    //删除
    @RequestMapping("delete")
    @ResponseBody
    @PermissionName("角色删除")
    @RequiresPermissions("role:delete")
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try{
            roleService.deleteByPrimaryKey(id);
        }catch(Exception e){
            e.printStackTrace();
            result.markMsg("删除失败");
        }
        return  result;
    }


}
